<template>
	<view class="costManage-container">
		<view class="date-choose-box">
			<image src="@/static/index/calendar.png" mode="widthFix" class="calendar-icon"></image>
			<picker mode="date" fields="month" :data-index='0' :value="starTime" @change="bindDateChange" :start="startLimit" :end="lastMonth">
				<view class="select-input" v-if="starTime">{{starTime}}</view>
				<view class="select-input" v-else>开始日期</view>
			</picker>
			~
			<picker mode="date" fields="month" :data-index='1' :value="date" @change="bindDateChange" :start="starTime" :end="lastMonth">
				<view class="select-input" v-if="endTime">{{endTime}}</view>
				<view class="select-input" v-else>结束日期</view>
			</picker>
		</view>
		
		<view class="cost-box">
			<view class="cost-item total-cost">
				<text class="value">{{info.totalCost||0}}</text>
				<text class="label">总成本</text>
			</view>
			<view class="item-box flexWrapNo">
				<view class="cost-item">
					<text class="value">{{info.tuanCharges||0}}</text>
					<text class="label">美团手续费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.douyinCharges||0}}</text>
					<text class="label">抖音手续费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.platformCharges||0}}</text>
					<text class="label">系统服务费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.bankCharges||0}}</text>
					<text class="label">银行手续费</text>
				</view>
			</view>
		</view>
		
		<view class="table-list">
			<view class="table-item table-head">
				<view class="date">时间</view>
				<view class="type">费用类型</view>
				<view class="money">费用金额</view>
				<view class="desc">费用说明</view>
				<view class="operate">操作</view>
			</view>
			<view class="table-item" v-for="(item,index) in list" :key="index">
				<view class="date">{{item.costDate}}</view>
				<view class="type">{{formatType(item.type)}}</view>
				<view class="money">{{item.amount}}</view>
				<view class="desc">{{item.desc}}</view>
				<view class="operate">
					<view @tap="lookAttachment(item)">查看附件</view>
				</view>
			</view>
		</view>
		
		<view class="quesheng" v-if="list.length==0">
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			const lastMonth = this.getLastMonth({
				format: true
			})
			return{
				startLimit:'',
				starTime:lastMonth,
				endTime:lastMonth,
				lastMonth:lastMonth,
				info:{},
				list:[],
				typeArray:[
					{label:'电费',value:1},
					{label:'水费',value:2},
					{label:'保洁费',value:3},
					{label:'维护费',value:4},
					{label:'房租',value:5},
					{label:'其他',value:9},
				],
				storeId:'',
			}
		},
		onLoad(options) {
			this.storeId=options.storeId
			this.getDividendDate()
		},
		onShow() {
			this.getList()
		},
		methods:{
			// 获取日期 --上月
			getLastMonth() {
			    const current = new Date();
			    current.setMonth(current.getMonth() - 1);
			    const year = current.getFullYear();
			    const month = (current.getMonth() + 1).toString().padStart(2, '0');
			    return `${year}-${month}`
			},
			
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			
			// 获取开始分红日期
			getDividendDate(){
				this.$request.get(this.$api.getDividendDate, {
					storeId:this.storeId,
					adminId:uni.getStorageSync('user_id'),
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;	
						this.startLimit=data
					}
				})
			},
			
			// 获取成本明细
			getList(){
				this.$request.post(this.$api.costList, {
					storeId:this.storeId,
					start:this.starTime,
					end:this.endTime,
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						this.info=data
						this.list=data.costList
					}
				})
			},
			
			// 回显费用类型
			formatType(value){
				let foundData = this.typeArray.find(item => item.value ==value)
				return foundData.label
			},
			
			// 查看附件
			lookAttachment(item){
				let imageList
				if(item.attachment){
					imageList=item.attachment.split(',')
					this.previewImage(0,imageList)
				}else{
					this.$tools.showToast('无附件可查看')
				}
			},
		},
	}
</script>

<style lang="scss" scoped>
	.costManage-container{
		padding: 0 28rpx;
		padding-bottom: 40rpx;
	}
	.date-choose-box{
		color: #FFFFFF;
		display: flex;
		align-items: center;
		gap:20rpx;
		padding: 28rpx 0 20rpx 0;
		font-size: 28rpx;
		background: #040336;
		position: sticky;
		top: 0;
		.calendar-icon{
			width: 36rpx;
		}
	}
	.cost-box{
		padding-bottom: 40rpx;
		border-bottom: 1rpx solid rgba(255,255,255,0.3);
		.cost-item{
			display: flex;
			flex-direction: column;
			align-items: center;
			.value{
				color:rgba(255,255,255,1);
				font-size: 28rpx;
				font-weight: bold;
			}
			.label{
				margin-top: 4rpx;
				color:rgba(255,255,255,0.8);
				font-size: 26rpx;
			}
		}
		.total-cost{
			.value{
				font-size: 34rpx;
			}
		}
		.item-box{
			margin-top: 20rpx;
			gap: 20rpx;
			justify-content: space-between;
		}
	}
	
	.table-list{
		margin-top: 30rpx;
		background: #1D1C5C;
		border-radius: 8rpx;
		.table-item{
			display: flex;
			color: #fff;
			font-size: 24rpx;
			text-align: center;
			border-bottom:1rpx solid  rgba(255,255,255,0.3);
			&:last-child{
				border-bottom: unset;
			}
			>view{
				padding: 16rpx 10rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.date{
				width: 160rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.type{
				width: 130rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.money{
				flex: 1;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.desc{
				width: 140rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.operate{
				width: 130rpx;
				>view{
					color: #26BCFD;
				}
			}
		}
		.table-head{
			>view{
				font-weight: bold;
				font-size: 26rpx !important;
				padding: 14rpx 10rpx;
			}
		}	
	}
	
	.quesheng{
		margin: 50rpx 0;
		display: flex;
		justify-content: center;
		image{
			width: 476upx;
			height: 280upx;
		}
	}
</style>